﻿using Microsoft.EntityFrameworkCore;

namespace 一对多
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (MyDBCotext ctx = new MyDBCotext())
            {
                //Article article = new Article()
                //{
                //    Title = "yhs太酷了",
                //    Content = "Test1",
                //};

                //Comment comment1 = new Comment()
                //{
                //    Message = "很厉害"
                //};
                //Comment comment2 = new Comment()
                //{
                //    Message = "很好看"
                //};
                //article.Comments.Add(comment1);//评论加入到文章
                //article.Comments.Add(comment2);



                //ctx.Articles.Add(article);//文章加入到表里

                //var test1 = ctx.Articles.FirstOrDefault(x => x.Id == 1);
                //var test1 = ctx.Articles.Include(a=>a.Comments).FirstOrDefault(x => x.Id == 1);//include查询一对多的数据
                //Console.WriteLine(test1.Id + "," + test1.Title);
                //foreach (var test2 in ctx.Comments)
                //{
                //    Console.WriteLine(test2.Id + "," + test2.Message);
                //}

                //var test1 = ctx.Comments.Include(a=>a.Article).FirstOrDefault(e => e.Id == 3);
                //Console.WriteLine(test1.Message);
                //Console.WriteLine(test1.Article.Title + test1.Article.Id);

                var test = ctx.Comments.Single(e => e.Id == 3);
                Console.WriteLine(test.Id + "," + test.ArticleId);
                
                //ctx.SaveChanges();
            }
                

            
        }
    }
}
